package com.cheyun.netsalev3.viewmodel

import android.util.Log
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.widget.MyDialog

class MyDialogViewModel(var title: String, var list: ArrayList<DialogParam>, var isMultiple:Boolean) : ViewModel() {
    var listdata = MutableLiveData<List<DialogParam>>()

    init {
        listdata.postValue(list)
    }

    fun onClickItem(view: View, item: DialogParam) {
        var temlist = listdata.value
        if (temlist != null) {
            for (i in temlist) {
                if(!isMultiple) {
                    i.IsSelect = i.id == item.id
                }else{
                    if(i.id == item.id){
                        i.IsSelect = !i.IsSelect
                    }
                }
            }
        }
    }


    fun getOkData(): DialogParam? {
        Log.e("cancel", list.toString())
        for (item in list) {
            if (item.IsSelect) {
                return item
            }
        }
        return null
    }

    fun getOkPosition(): Int {
        for (i in 0 until list.size) {
            var item = list[i]
            if (item.IsSelect) {
                return i
            }
        }
        return -1
    }

    fun getOkDataList(): List<DialogParam>? {
        Log.e("cancel", list.toString())
        var items:ArrayList<DialogParam> = arrayListOf()
        for (item in list) {
            if (item.IsSelect) {
                items.add(item)
            }
        }
        return items
    }


}